package com.itheima.my_reggie.web.exception;

import com.itheima.my_reggie.common.CustomException;
import com.itheima.my_reggie.common.Result;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLIntegrityConstraintViolationException;

//设置统一的数据异常类
@RestControllerAdvice
public class GlobalExeptionHandler {
    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public Result doSQLConstraintException(SQLIntegrityConstraintViolationException exception){
//      若在添加的时候添加时数据库返回SQLIntegrityConstraintViolationException的异常，则捕获该异常，并把异常信息返回值前台
        return Result.error(exception.getMessage());
    }

    @ExceptionHandler(CustomException.class)
    public Result doCustomException(CustomException exception){
//      若在删除分类时，分类下存在此菜品，删除时抛出此异常，则捕获该异常，并把异常信息返回值前台
        return Result.error(exception.getMessage());
    }
}
